import 'package:flutter/cupertino.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:kq_flutter_core_widget/widgets/button/kq_ink_well.dart';
import 'package:kq_flutter_core_widget/widgets/image/kq_image.dart';
import 'package:kq_flutter_tv_widgets/widgets/stackView/stack_view_widget.dart';
import '../../../../resources/images.dart';
import '../../config/kq_global.dart';
import '../../resources/kq_images.dart';
import '../../resources/kq_theme_colors.dart';

abstract class StackViewChildWidget extends StatelessWidget {
  final StackViewWidgetState state;

  ///自定义头部导航
  final Widget? Function(BuildContext context)? titleBuild;

  ///是否显示默认头部
  final bool isShowTitle;

  ///默认头部title
  final String? title;

  ///头部右侧widget
  final Widget Function(BuildContext context)? buildTitleRight;

  ///是否显示返回按钮
  final bool isShowBackIcon;

  ///是否显示关闭按钮
  final bool isShowCloseIcon;

  const StackViewChildWidget({
    super.key,
    required this.state,
    this.titleBuild,
    this.isShowTitle = true,
    this.title,
    this.buildTitleRight,
    this.isShowBackIcon = true,
    this.isShowCloseIcon = true,
  });

  @override
  @mustCallSuper
  Widget build(BuildContext context) {
    return Column(
      children: [
        _title(context),
        Expanded(
          child: buildChildPage(context),
        ),
      ],
    );
  }

  Widget buildChildPage(BuildContext context);

  ///回退，返回上一个界面。
  ///[bool] 返回true表示成功返回上一级，
  ///返回false表示返回失败，已是最后一个界面，不可继续返回。
  bool? back({
    dynamic result,
    bool showAnimate = true,
  }) {
    if (state.backAble()) {
      return state.back(
        result: result,
        showAnimate: showAnimate,
      );
    } else {
      state.close(result: result);
      return false;
    }
  }

  ///关闭，显示空界面
  close({dynamic result}) {
    state.close(result: result);
  }

  ///跳转指定界面
  ///[page] 需要打开的界面。
  ///[arguments] 打开时需要给下一个界面传递的参数。
  ///[callback]打开的界面关闭时，回传的参数。
  open(
    StackViewChildWidget widget, {
    dynamic arguments,
    Function(dynamic result)? callback,
    bool showAnimate = true,
  }) {
    state.open(
      widget,
      arguments: arguments,
      callback: callback,
      showAnimate: showAnimate,
    );
  }

  ///获取参数
  dynamic getArguments() {
    return state.getArguments();
  }

  ///根据[key]获取参数，只支持Map传参方式
  S? getArg<S>(String key) {
    return state.getArg(key);
  }

  _title(BuildContext context) {
    if (titleBuild != null) {
      if (isShowTitle) {
        return titleBuild?.call(context) ?? _buildDefaultTitle(context);
      } else {
        return titleBuild?.call(context) ?? Container();
      }
    } else if (isShowTitle) {
      return _buildDefaultTitle(context);
    } else {
      return Container();
    }
  }

  Widget _buildDefaultTitle(BuildContext context) {
    return Container(
      width: double.infinity,
      height: 55.r,
      color: const Color(0xFFEAEEF2),
      child: Row(
        children: [
          Visibility(
            visible: state.backAble() && isShowBackIcon,
            child: KqInkWell(
              horizontalPadding: 6.r,
              onTap: () {
                state.back();
              },
              child: KqImage.assets(
                url: KqImages.commonLeft,
                package: KqGlobal.packageName,
                width: 28.r,
                height: double.infinity,
                fit: BoxFit.contain,
              ),
            ),
          ),
          Text(
            title ?? "",
            style: TextStyle(
              fontSize: 10.r,
              color: KqThemeColors.text26,
            ),
          ),
          Expanded(child: buildTitleRight?.call(context) ?? Container()),
          Visibility(
            visible: !state.backAble() && isShowCloseIcon,
            child: KqInkWell(
              horizontalPadding: 6.r,
              onTap: () {
                state.close();
              },
              child: KqImage.assets(
                url: Images.commonIcGuanbi,
                package: KqGlobal.packageName,
                width: 28.r,
                height: double.infinity,
                fit: BoxFit.contain,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
